package widget.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import net.ck.hzq.R;

import java.util.ArrayList;

public class InputPwdDialog extends MyDialog implements View.OnClickListener {
    private static final String TAG = "InputPwdDialog";
    private ArrayList<EditText> array = new ArrayList<EditText>();
    private EditText mEdt_parking_one;
    private EditText mEdt_parking_two;
    private EditText mEdt_parking_three;
    private EditText mEdt_parking_four;
    private EditText mEdt_parking_five;
    private EditText mEdt_parking_six;
    private TextWatcher tw_pwd;
    private String inputNumber = "";
    private onKeyListeners onkeylistener;
    private int count = 0;

    private Context mContext;

    private DialogCallback callback;

    private OnEditTextListener onEditTextListener;

    private Button submit;


    public OnEditTextListener getOnEditTextListener() {
        return onEditTextListener;
    }

    public void setOnEditTextListener(OnEditTextListener onEditTextListener) {
        this.onEditTextListener = onEditTextListener;
    }


    public interface OnEditTextListener {
        void inputComplete(int state, String password);
    }

    public InputPwdDialog(@NonNull Context context, DialogCallback callback) {
        this(context, R.style.MyDialog, callback);
    }

    public InputPwdDialog(@NonNull Context context, int themeResId, DialogCallback callback) {
        super(context, themeResId);
        mContext = context;
        this.callback = callback;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: ");
        initView();
    }


    private void initView() {
        setContentView(R.layout.dialog_pwd_input);
        WindowManager windowManager = getWindow().getWindowManager();
        int screenWidth = windowManager.getDefaultDisplay().getWidth();
        int screenHeight = windowManager.getDefaultDisplay().getHeight();
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
//        attributes.alpha = 0.3f;
        attributes.width = screenWidth;
        attributes.height = screenHeight;
        attributes.gravity = Gravity.CENTER;
        getWindow().setAttributes(attributes);
        setCancelable(true);
        mEdt_parking_one = findViewById(R.id.mEdt_parking_one);
        mEdt_parking_two = findViewById(R.id.mEdt_parking_two);
        mEdt_parking_three = findViewById(R.id.mEdt_parking_three);
        mEdt_parking_four = findViewById(R.id.mEdt_parking_four);
        mEdt_parking_five = findViewById(R.id.mEdt_parking_five);
        mEdt_parking_six = findViewById(R.id.mEdt_parking_six);
        mEdt_parking_two.setFocusable(false);
        mEdt_parking_five.setFocusable(false);
        mEdt_parking_four.setFocusable(false);
        mEdt_parking_three.setFocusable(false);
        mEdt_parking_six.setFocusable(false);
        array.add(mEdt_parking_one);
        array.add(mEdt_parking_two);
        array.add(mEdt_parking_three);
        array.add(mEdt_parking_four);
        array.add(mEdt_parking_five);
        array.add(mEdt_parking_six);
        submit = findViewById(R.id.btn_submit);
        submit.setOnClickListener(this);
        findViewById(R.id.btn_forget).setOnClickListener(this);
        findViewById(R.id.layout_pwd_close).setOnClickListener(this);
        setListener(mContext);
    }

    @SuppressWarnings("unused")
    private void setFocusToView(int target) {
        for (int i = 0; i < array.size(); i++) {
            array.get(i).setFocusable(true);
            array.get(i).setFocusableInTouchMode(true);
            if (i != target) {
                array.get(i).setFocusable(false);
            }
        }
    }

    private void setListener(Context context) {
        onkeylistener = new onKeyListeners();
        editPwdWatcher(context);
        //设置字符改变监听
        mEdt_parking_one.addTextChangedListener(tw_pwd);
        mEdt_parking_two.addTextChangedListener(tw_pwd);
        mEdt_parking_three.addTextChangedListener(tw_pwd);
        mEdt_parking_four.addTextChangedListener(tw_pwd);
        mEdt_parking_five.addTextChangedListener(tw_pwd);
        mEdt_parking_six.addTextChangedListener(tw_pwd);
        //删除按钮监听
        mEdt_parking_one.setOnKeyListener(onkeylistener);
        mEdt_parking_two.setOnKeyListener(onkeylistener);
        mEdt_parking_three.setOnKeyListener(onkeylistener);
        mEdt_parking_four.setOnKeyListener(onkeylistener);
        mEdt_parking_five.setOnKeyListener(onkeylistener);
        mEdt_parking_six.setOnKeyListener(onkeylistener);
    }

    /**
     * 字符改变监听
     *
     * @param context
     */
    private void editPwdWatcher(final Context context) {
        tw_pwd = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 1) {
                    if (mEdt_parking_one.isFocusable()) {
                        mEdt_parking_two.setFocusable(true);
                        mEdt_parking_two.setFocusableInTouchMode(true);
                    } else if (mEdt_parking_two.isFocusable()) {
                        mEdt_parking_three.setFocusable(true);
                        mEdt_parking_three.setFocusableInTouchMode(true);
                    } else if (mEdt_parking_three.isFocusable()) {
                        mEdt_parking_four.setFocusable(true);
                        mEdt_parking_four.setFocusableInTouchMode(true);
                    } else if (mEdt_parking_four.isFocusable()) {
                        mEdt_parking_five.setFocusable(true);
                        mEdt_parking_five.setFocusableInTouchMode(true);
                    } else if (mEdt_parking_five.isFocusable()) {
                        mEdt_parking_six.setFocusable(true);
                        mEdt_parking_six.setFocusableInTouchMode(true);
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 1) {
                    if (mEdt_parking_one.isFocused()) {
                        mEdt_parking_one.setFocusable(false);
                        mEdt_parking_two.requestFocus();
                    } else if (mEdt_parking_two.isFocused()) {
                        mEdt_parking_two.setFocusable(false);
                        mEdt_parking_three.requestFocus();
                    } else if (mEdt_parking_three.isFocused()) {
                        mEdt_parking_three.setFocusable(false);
                        mEdt_parking_four.requestFocus();
                    } else if (mEdt_parking_four.isFocused()) {
                        mEdt_parking_four.setFocusable(false);
                        mEdt_parking_five.requestFocus();
                    } else if (mEdt_parking_five.isFocused()) {
                        mEdt_parking_five.setFocusable(false);
                        mEdt_parking_six.requestFocus();
                    } else if (mEdt_parking_six.isFocused()) {
                        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(mEdt_parking_six.getWindowToken(), 0);
                        inputNumber = getEditNumber();
                        Log.i(TAG, inputNumber.length() + "");
                        if (onEditTextListener != null) {
                            onEditTextListener.inputComplete(1, inputNumber);
                        }
                    }
                }
            }
        };

    }

    /**
     * 删除按钮监听
     */
    class onKeyListeners implements View.OnKeyListener {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                count++;
                if (count < 2) {
                    return false;
                }
                count = 0;
                inputNumber = "";
                if (mEdt_parking_six.isFocused()) {
                    mEdt_parking_five.setFocusableInTouchMode(true);
                    if (mEdt_parking_six.getText().toString().length() == 1) {
                        mEdt_parking_six.getText().clear();
                        mEdt_parking_six.requestFocus();
                        return true;
                    } else {
                        mEdt_parking_six.clearFocus();
                        mEdt_parking_five.getText().clear();
                        mEdt_parking_six.setFocusable(false);
                        mEdt_parking_five.requestFocus();
                    }
                } else if (mEdt_parking_five.isFocused()) {
                    mEdt_parking_five.clearFocus();
                    mEdt_parking_five.setFocusable(false);
                    //mEdt_parking_four.setFocusable(true);
                    mEdt_parking_four.setFocusableInTouchMode(true);
                    mEdt_parking_four.getText().clear();
                    mEdt_parking_four.requestFocus();
                } else if (mEdt_parking_four.isFocused()) {
                    mEdt_parking_four.clearFocus();
                    mEdt_parking_four.setFocusable(false);
                    //mEdt_parking_three.setFocusable(true);
                    mEdt_parking_three.setFocusableInTouchMode(true);
                    mEdt_parking_three.getText().clear();
                    mEdt_parking_three.requestFocus();
                } else if (mEdt_parking_three.isFocused()) {
                    mEdt_parking_three.clearFocus();
                    mEdt_parking_three.setFocusable(false);
                    mEdt_parking_two.setFocusableInTouchMode(true);
                    mEdt_parking_two.getText().clear();
                    mEdt_parking_two.requestFocus();
                } else if (mEdt_parking_two.isFocused()) {
                    mEdt_parking_two.clearFocus();
                    mEdt_parking_two.setFocusable(false);
                    //mEdt_parking_one.setFocusable(true);
                    mEdt_parking_one.setFocusableInTouchMode(true);
                    mEdt_parking_one.getText().clear();
                    mEdt_parking_one.requestFocus();
                }
            }
            return false;
        }

    }

    public String getEditNumber() {
        String number = mEdt_parking_one.getText().toString();
        number += mEdt_parking_two.getText().toString();
        number += mEdt_parking_three.getText().toString();
        number += mEdt_parking_four.getText().toString();
        number += mEdt_parking_five.getText().toString();
        number += mEdt_parking_six.getText().toString();
        return number;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                submit.setEnabled(false);
                if (callback != null)
                    callback.OnConfirm();
                break;
            case R.id.btn_forget:
                if (callback != null)
                    callback.OnConfirm1();
                break;
            case R.id.layout_pwd_close:
                submit.setEnabled(true);
                this.dismiss();
                break;
            default:
                break;
        }
    }

    public void setSubmitEnable(boolean enable) {
        submit.setEnabled(enable);
    }

    public void setClearText() {
        mEdt_parking_one.getText().clear();
        mEdt_parking_two.getText().clear();
        mEdt_parking_three.getText().clear();
        mEdt_parking_four.getText().clear();
        mEdt_parking_five.getText().clear();
        mEdt_parking_six.getText().clear();
        mEdt_parking_one.requestFocus();
        mEdt_parking_two.setFocusable(true);
        mEdt_parking_one.setFocusableInTouchMode(true);
    }

}
	
